package com.luxsoft.sw3.bi.alta;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JComponent;
import javax.swing.JPanel;

import net.infonode.docking.DockingWindowAdapter;
import net.infonode.docking.DockingWindowListener;
import net.infonode.docking.RootWindow;
import net.infonode.docking.View;
import net.infonode.docking.util.DockingUtil;
import net.infonode.docking.util.StringViewMap;
import net.infonode.docking.util.ViewMap;


import com.luxsoft.siipap.swing.browser.BrowserPanel;
import com.luxsoft.siipap.swing.browser.FilteredBrowserPanel;
import com.luxsoft.siipap.swing.utils.DockingUtils;



public class AltAMainWindow extends JPanel{
	
	private final AltAController controller;
	
	private AltAHeader header;
	private JPanel documentPanel;
	
	public AltAMainWindow(final AltAController controller){
		this.controller=controller;		
		initComponents();
	}
	
	private void initComponents(){
		setLayout(new BorderLayout());
		getDocumentPanel();
	}

	@Override
	public void addNotify() {		
		super.addNotify();
		add(getHeaderPanel(),BorderLayout.NORTH);
		add(getDocumentPanel(),BorderLayout.CENTER);
	}
	
	private StringViewMap viewMap;
	protected RootWindow rootWindow;
	private DockingWindowListener dockingHandler;
	
	/**
	 * Crea el panel de documentos o vistas internas, Esta implementacion crea un TabbedPanel
	 * que es accesible para las sub-clases mediante la  variable tabPanel
	 * 
	 * @return
	 */
	protected JComponent getDocumentPanel(){
		if(documentPanel==null){
			documentPanel=new JPanel(new BorderLayout());
			viewMap=new StringViewMap();
			rootWindow=new RootWindow(viewMap);
			dockingHandler=new DockingHandler();
			DockingUtils.configRootWindow(rootWindow);
			DockingUtils.configTabWindowProperties(rootWindow);
			rootWindow.addListener(dockingHandler);
			documentPanel.add(rootWindow,BorderLayout.CENTER);
			documentPanel.setPreferredSize(new Dimension(950,600));
		}		
		return documentPanel;
	}
	
	private JComponent getHeaderPanel(){
		if(header==null){
			header=new AltAHeader();
			controller.registrarModelListener(header);
		}		
		return header;
	}
	
	public void addView(final View view,String id){
		DockingUtil.addWindow(view,rootWindow);
		viewMap.addView(id,view);
		view.restoreFocus();
	}
	
	public void addPanel(final FilteredBrowserPanel panel,String id){
		View view=viewMap.getView(id);
		if(view==null){
			view=new View(panel.getTitle(),null,panel.getControl());
		}
		addView(view,id);
	}
	
	protected class DockingHandler extends DockingWindowAdapter{
		
	}

}
